<?php
/**
 * Created by PhpStorm.
 * User: zouhua
 * Date: 2017/6/19
 * Time: 15:56
 */

namespace app\actions;


use app\models\Like;

class LikeAction extends BaseUserAction {

    public $own;

    public function run($id) {
        /* @var $modelClass \app\components\BaseActiveRecord */
        $modelClass = $this->modelClass;
        $model = $modelClass::findOne($id);
        if (!$model) {
            return $this->controller->error('网络异常');
        }

        if (Like::getIsLike($this->own, $id, $this->controller->getLikeId())) {
            return $this->controller->error('点过赞了');
        }

        $like = new Like();
        $like->own = $this->own;
        $like->own_id = $id;
        $like->uid = $this->controller->getLikeId();
        $like->save();

        $likes = $model->getAttribute('likes') + 1;
        $model->setAttribute('likes', $likes);
        return $model->save(FALSE);
    }
}